import 'package:flutter/material.dart';
import '../pages/yun_xfx.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:dio/dio.dart';
import 'dart:convert';
import 'package:flutter_easyloading/flutter_easyloading.dart';

class LoginPage extends StatefulWidget {
  const LoginPage({Key? key}) : super(key: key);

  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  late TextEditingController _editingController;
  late TextEditingController _editingController2;
  final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();

  @override
  Widget build(BuildContext context) {
    _editingController = new TextEditingController(text: "gys001");
    _editingController2 = new TextEditingController(text: "666666");
    return Scaffold(
      key: _scaffoldKey,
      backgroundColor: Colors.grey[200],
      body: Column(
        children: [
          Stack(
            children: [
              Image.asset(
                "assets/imgs/bg-top.png",
                width: 750.w,
                height: 438.h,
              ),
              Positioned(
                left: 77.w,
                top: 200.h,
                child: Text(
                  "用户登录",
                  style: TextStyle(color: Colors.white, fontSize: 54.sp),
                ),
              )
            ],
          ),
          SizedBox(
            height: 30.w,
          ),
          _input(Icons.account_box, "账号", false, _editingController),
          SizedBox(
            height: 50.w,
          ),
          _input(Icons.lock, "密码", true, _editingController2),
          SizedBox(
            height: 60.w,
          ),
          _button(context),
          Expanded(
            child: _footer(),
          )
        ],
      ),
    );
  }

  Widget _button(context) {
    return InkWell(
      onTap: () {
        _login(context);
      },
      child: Container(
        width: 190.w,
        height: 190.w,
        child: Icon(
          Icons.arrow_forward,
          size: 75.sp,
          color: Colors.white,
        ),
        decoration: BoxDecoration(
          color: Colors.blue,
          borderRadius: BorderRadius.all(
            Radius.circular(95.w),
          ),
          gradient: LinearGradient(
            begin: Alignment.bottomLeft,
            end: Alignment.topRight,
            colors: [Color(0xFF1989FA), Color(0xFF6FB0FE)],
          ),
        ),
      ),
    );
  }

  Widget _input(icon, label, obscureText, controller) {
    return Container(
      margin: EdgeInsets.only(left: 46.w, right: 46.w),
      padding: EdgeInsets.only(left: 28.w),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.all(Radius.circular(20.w)),
        color: Colors.white,
      ),
      child: Row(
        children: [
          Icon(
            icon,
            size: 34.sp,
            color: Colors.blue,
          ),
          SizedBox(
            width: 14.w,
          ),
          Text(label, style: TextStyle(color: Colors.grey[400], fontSize: 32.sp)),
          SizedBox(
            width: 14.w,
          ),
          Expanded(
            child: TextField(
              obscureText: obscureText,
              controller: controller,
              style: TextStyle(fontSize: 30.sp),
              decoration: InputDecoration(
                border: InputBorder.none,
              ),
            ),
          )
        ],
      ),
    );
  }

  Widget _footer() {
    return Column(
      mainAxisAlignment: MainAxisAlignment.end,
      children: [
        Image.asset(
          "assets/imgs/login_boomin.png",
          width: 98.w,
          height: 36.h,
        ),
        SizedBox(
          height: 14.h,
        ),
        Text("v1.0.1.201714_bete",
            style: TextStyle(
              fontSize: 24.sp,
              color: Color.fromRGBO(200, 200, 200, 0.5),
            )),
        SizedBox(
          height: 48.h,
        )
      ],
    );
  }

  void _login(context) async {
    try {
      var username = _editingController.text.trim();
      var pwd = _editingController2.text.trim();
      if (username == null || username.isEmpty) {
        final snackBar = SnackBar(
          content: const Text('请输入账号'),
        );
        ScaffoldMessenger.of(context).showSnackBar(snackBar);
      } else if (pwd == null || pwd.isEmpty) {
        final snackBar = SnackBar(
          content: const Text('请输入密码'),
        );
        ScaffoldMessenger.of(context).showSnackBar(snackBar);
      } else {
        // EasyLoading.init();
        // EasyLoading.show(status: '登录中...');
        dynamic response = await Dio().post(
          'http://scs.zn-nh.com/api/Android/Login',
          data: {'username': username, "pwd": pwd},
        );
        String s = response.toString();
        Map<String, dynamic> result = json.decode(s);
        var status = result["Data"]["status"];
        if (status == true) {
          Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) {
            return Yunxfx();
          }));
        }
      }
    } catch (e) {
      print(e);
    }
  }
}
